package com.cy.common.config;


import java.util.LinkedHashMap;

import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**  
 * @Title:  SpringShiroConfig.java   
 * @Package com.cy.pj.common.config   
 * @Description:    描述   
 * @author: ZHANGSIMIN     
 * @date:   2019年10月18日 下午4:53:00   
 * @version V1.0 
 */
@Configuration
public class SpringShiroConfig {
	@Bean
	public SecurityManager newSecurityManager(@Autowired Realm realm) {
		DefaultWebSecurityManager sManager=
				new DefaultWebSecurityManager();
		sManager.setRealm(realm);
		return sManager;
	}

	@Bean("shiroFilterFactory")
	public ShiroFilterFactoryBean newShiroFilterFactoryBean(
			@Autowired SecurityManager securityManager) {
		ShiroFilterFactoryBean sfBean=
				new ShiroFilterFactoryBean();
		sfBean.setSecurityManager(securityManager);
		//假如没有认证请求先访问此认证的url
		sfBean.setLoginUrl("/doLoginUI");
		//定义map指定请求过滤规则(哪些资源允许匿名访问,哪些必须认证访问)
		LinkedHashMap<String,String> map=
				new LinkedHashMap<>();
		//静态资源允许匿名访问:"anon"
		map.put("/assets/**","anon");
		//放行登录,注册,注销,首页,页面头资源,页面尾资源请求
		map.put("/","anon");
		map.put("/index","anon");
		map.put("/indexPage/**","anon");
		map.put("/newsletter-area","anon");
		map.put("/header","anon");
		map.put("/footer","anon");
		map.put("/doRegUI","anon");
		map.put("/doLoginUI","anon");
		map.put("/user/doLogin","anon");
		map.put("/user/doReg","anon");
		map.put("/Cate/getIndexCate", "anon");
		map.put("/doLogout","logout");
		//除了匿名访问的资源,其它都要认证("authc")后访问
		map.put("/**","authc");
		sfBean.setFilterChainDefinitionMap(map);
		return sfBean;
	}

}
